<?php

namespace app\admin\model;
use think\Session;
use think\Model;

class Classschedule extends Model
{
    // 表名
    protected $name = 'classschedule';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    
    // 追加属性
    protected $append = [
        'starttime_text',
        'endtime_text',
        'status_text'
    ];
    
     // 定义全局的查询范围
     protected function base($query)
     {
        //学校筛选
        $admin =Session::get("admin");
        if($admin["branchids"]!=null && $admin["branchids"]!='')
        {
            $query->where('branchids','in',$admin["branchids"]);     
        }
     }
    
    public function getStatusList()
    {
        return ['0' => __('Status 0'),'1' => __('Status 1'),'2' => __('Status 2')];
    }    
    
    public function getWeekList()
    {
        return ['1' => __('Monday'),'2' => __('Tuesday'),'3' => __('Wednesday'),'4' => __('Thusday'),'5' => __('Friday'),'6' => __('Saturday'),'7' => __('Sunday')];
    }    


    public function getStarttimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['starttime'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getEndtimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['endtime'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['status'];
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getWeekTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['week'];
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getRenderAttr($value, $data)
    {
        $branchname = Model('Branch')->where('id',$data['branchids'])->value('name');
        $classname = Model('Classes')->where('id',$data['classid'])->value('name');
        $classroomname = Model('Classroom')->where('id',$data['classid'])->value('name');
        $teachername = Model('Admin')->where('id',$data['teacherid'])->value('nickname');
        $allDay = ($data['starttime'] === $data['endtime'] && date("H:i:s", $data['starttime']) == '00:00:00');
        return [
            'id'              => $data['id'],
            'title'           => '['.$branchname.'] '.'-'.$classname.'-'.$classroomname.' - '.$teachername,
            'start'           => date("c", $data['starttime']),
            'end'             => date("c", $data['endtime']),
            'backgroundColor' => '#3c8dbc',
            'borderColor'     => '',
            'allDay'          => $allDay,
            'url'             => 'schedule/view/ids/'.$data['id'],
            'className'       => "btn-dialog fc-{$data['status']}" . (($allDay ? $data['endtime'] + 86400 : $data['endtime']) < time() ? ' fc-expired' : '')
        ];
    }

    protected function setStarttimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setEndtimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }


}
